// Utility.keyConvert();
// const smcId = hardware.smartCard.serialNumber;
// EVM.cookie.setValue('smcId', smcId);
var indexR = 0;
var data = [
    {
        name: "历史抽奖",
        url:"monthlist.html",
    },
    {
        name: "进入答题",
        url:"da-ti.html",
    },
    {
        name: "退出",
        url: "./index.html",
    }
];

function prompt(title) {
    /*str = '<div class="tishi" style="box-sizing:border-box;font-size: 30px;z-index: 1111;text-align: ' +
        'center;width: 500px;height: 120px;line-height:120px;position: absolute;background:#ffe488;border-radius: 20px;top: 280px;left: 390px;color: black;">' + title +
        '</div>';
    $("body").append(str);
    setTimeout(function () {
        $('.tishi').hide()
    }, 2000);*/

    var mydiv = document.createElement('div');
    mydiv.className = 'tishi';
    mydiv.style.boxSizing = 'border-box';
    mydiv.style.fontSize = '30px';
    mydiv.style.width = '800px';
    mydiv.style.height = '120px';
    mydiv.style.position = 'absolute';
    mydiv.style.background = '#ffe488';
    mydiv.style.zIndex = 1111;
    mydiv.style.top = '280px';
    mydiv.style.left = '300px';
    mydiv.style.lineHeight = '120px';
    mydiv.style.textAlign = 'center';
    mydiv.style.color = 'black';
    mydiv.style.borderRadius = '20px';
    mydiv.innerText = title;
    //mydiv.innerText = title;

//$("body").append(str);
document.body.appendChild(mydiv);

setTimeout(function () {
    //document.getElementsByClassName('tishi')[0].style.display='none';
    document.body.removeChild(mydiv);
}, 20000);
};
var backurl = getParam('backurl');
var hasIndex=EVM.cookie.getValue('sdzIndex');
// console.log(hasIndex, 'hasIndex');
var totalPage = 1
var page = 1
var pagesize = 4
function requestData(){
    var httpurl = api+Record;
    //prompt('zhixing???=====>>>'+httpurl);
    /*http(api+Record,{page:page,pagesize:pagesize},function(data){
        //$("#content").html('');
        prompt('zhixing le');
        document.getElementById('content').innerHTML = '';
        var str = ""
        for(var i=0;i<data.record.length;i++){
            str += "<li>"
            var mobile = handleMobile(data.record[i].mobile);
            str += data.record[i].prize_name + "&nbsp;&nbsp;&nbsp;&nbsp;"+mobile+"&nbsp;&nbsp;"+data.record[i].tv_no.substr(0,4)+"*******"+data.record[i].tv_no.substr(18)
            str += "</li>"
        }
        //$("#content").html(str)
        document.getElementById('content').innerHTML = str;
        totalPage = data.totalpage
    })*/


    var ajax;
    if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject('Microsoft.XMLHTTP');
    }
    ajax.onreadystatechange = function () {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var text = ajax.responseText;
            if (text.length != 0) {
                var response1 = eval("(" + text + ")");
            }

            //prompt(text);

            var str = "";
            for(var i=0;i<response1.record.length;i++){
                var response = response1.record;
                str += "<li>"
                var mobile = handleMobile(response[i].mobile);
                str += response[i].prize_name + "&nbsp;&nbsp;&nbsp;&nbsp;"+mobile+"&nbsp;&nbsp;"+response[i].tv_no.substr(0,4)+"*******"+response[i].tv_no.substr(18)
                str += "</li>"
            }
            document.getElementById('content').innerHTML = str;
            totalPage = response1.totalpage
        }
    }
    ajax.open('post',api+Record, true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send("page=" + page + "&pagesize=" + pagesize);
}
//prompt("tv_no::::"+tv_no);
requestData()
// 处理手机号
function handleMobile(nums){
    if(nums.length > 10){
        var mobile = nums.substr(0,3)+"****"+nums.substr(7)
        return mobile
    }else{
        return "手机格式不正确"
    }
}

//获取机顶盒或浏览器信息
var env = evm.getStbBrowser();
function cookie(k, v) {
    if (v != undefined && typeof v == 'string') {
        evm.cookie(k, v);
        return;
    }
    if (!!evm.cookie(k)) {
        return evm.cookie(k);
    }
}
/*
 * $cmsIndex首页视图模块
 * 依赖:
 */
var cmsIndex = (function () {
    var mod = {};

    //配置项
    var config = mod.config = {
        //开启cookie开关
        cookieSwitch: true,
        cookieKey: 'cmsIndex',
        //模拟数据配置
        mockData: {
            //URI查询标示符
            key: '[mdata]',
            //文件目录
            dir: '../source/mockData'
        },
        //调试输出开关
        debug: false
    };

    var localData = mod.localData = {
        indexSource: '[mdata]/index.js'
    };
    //接口数据缓存
    var jsonCache = {};

    //各模块焦点切换的配置
    var modules = mod.modules = [];

    //当前模块的索引值,被引用的模块
    moduleId = 0,
        module = null,
        moduleGuid = 0,
        moduleCount = -1;

    mod.addModule = function () {
        return moduleCount += 1;
    };

    //moduleGuid存取器
    mod.moduleGuid = function (id) {
        if (typeof id == 'number') moduleGuid = id;
        return moduleGuid;
    };

    if (config.cookieSwitch && config.cookieKey) {
        //read cookie
        var pageCookie = cookie(config.cookieKey);
        if (pageCookie) {
            pageCookie = pageCookie.split(',');

            if (pageCookie.length > 0) {
                var moduleCookie = mod.moduleCookie = pageCookie[0].split('.');

                if (pageCookie[1]) {
                    var viewportCookie = mod.viewportCookie = moduleCookie;
                    moduleCookie = mod.moduleCookie = pageCookie[1].split('.');

                }
                if (pageCookie[2]) {
                    var parentCookie = mod.parentCookie = pageCookie[2].split('.');
                }
            }
        }

        //清除cookie
        evm.cutCookie(config.cookieKey);
    }
    //添加视图按键管理器
    mod.addListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'add',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(mod)
    //移除视图按键管理器
    mod.removeListener = function () {
        evm.controller.subscribe({
            type: 1,
            method: 'remove',
            callback: mod.grabEvent,
            context: mod
        });
    };
    // console.log(module)
    //视图按键管理

    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;

        if (module && module.grabEvent) {
            isBlock = module.grabEvent(key) || 0;
        }

        return isBlock;
    };
    //模块存取器
    mod.module = function (mID) {
        if (modules[mID]) {
            module = modules[mID].module || null;
        }

        return module;
    };

    //切换当前模块
    mod.handle = function (directive) {
        var me = mod;
        if (modules[moduleId].map &&
            (typeof modules[moduleId].map[directive] == 'number')) {

            moduleId = modules[moduleId].map[directive];

            return me.module(moduleId);
        }
    };
    mod.paint = function () {
        var me = mod;
        // getMessage();
        me.addListener();
        return me;
    };

    mod.run = function () {
        var me = mod;
        //默认moduleId = 0 导航栏
        //cookie得到焦点

        var nav = mod.module(moduleId);
        cmsIndex.handle('right');
        right.toggleFocus();
        //控制器管理按键
        evm.on(document, (!env.stb ? 'keydown' : (env.stb == 'iPanel' ?
            'irkeypress' : 'keydown')), evm.controller, evm);

        me.paint();

        return me;
    };
    return mod;

})();


/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 1
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 0, //右侧选中地址位置
        listName = 'model',
        length = 3,
        containerId = 'bottom',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'huodong';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function (hasIndex) {
        var me = mod;

        document.getElementById('history').style.backgroundImage = "url('./images/history.png')";
        document.getElementById('prepage').style.backgroundImage = "url('./images/record_top.png')";
        document.getElementById('nextpage').style.backgroundImage = "url('./images/record_bottom.png')";

        if(index == 0){
            document.getElementById('history').style.backgroundImage = "url('./images/history_active.png')";
            document.getElementById('answer').style.backgroundImage = "url('./images/enterQuestion.png')";
            document.getElementById('exitrecord').style.backgroundImage = "url('./images/exit.png')";
    
        }else if(index == 1){
            document.getElementById('history').style.backgroundImage = "url('./images/history.png')";
            document.getElementById('answer').style.backgroundImage = "url('./images/enterQuestion_active.png')";
            document.getElementById('exitrecord').style.backgroundImage = "url('./images/exit.png')";
        }else if(index == 2){
            document.getElementById('history').style.backgroundImage = "url('./images/history.png')";
            document.getElementById('answer').style.backgroundImage = "url('./images/enterQuestion.png')";
            document.getElementById('exitrecord').style.backgroundImage = "url('./images/exit_active.png')";
        }
        /*var elem = $("#" + containerId).children("." + listName)
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (hasIndex) {
            index = parseInt(hasIndex);
            // EVM.cookie.deleteValue('sdzIndex');
            hasIndex = '';
            if (index != length) {
                if (document.getElementsByClassName("active").length > 0) {
                    document.getElementsByClassName("active")[0].className = "model";
                }
            } else {
                index = 0;
            }
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }*/
        return me;
    };

    mod.clearCss = function () {
		var me = mod;
        var elem = $("#" + containerId).children("." + listName)
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
		return me;
	};
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        console.log(index)
        if (directive == "backwards") { //左
            if(index>0){
                index --      
            }
        }else if (directive == 'forwards') {  //右
            if(index<3){
                    index ++
            }
        }
        mod.toggleFocus()
        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
            right.clearCss()
            cmsIndex.handle('left')
            left.toggleFocus(1)
        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                if (!backurl) {
                        // alert(111)
                       history.go(-1);
                    } else {
                        window.location.href = decodeURIComponent(backurl);
                    }
                break;
            case 'enter':
           // EVM.cookie.setValue('sdzIndex',index)
               if(index == 0){
                    window.location.href = data[index].url
               }else if(index==1){
                    window.location.href = data[index].url
               }else if(index == 2){
                    // prompt(decodeURIComponent(backurl));
                    if (!backurl) {
                        // alert(111)
                       history.go(-1);
                    } else {
                        window.location.href = decodeURIComponent(backurl);
                    }
               }

            break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();


cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'left',
    modNum: cmsIndex.moduleGuid(),
    map: {
        right: 0
    }
};

var left = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {},
        index = 1, //右侧选中地址位置
        listName = 'model',
        length = 2,
        containerId = 'top',
        checkCookie = true; //配置是否定位焦点到二级栏目，默认否
    mod.modName = 'left';
    mod.modNum = cmsIndex.moduleGuid();
    mod.toggleFocus = function () {
        //prompt('hasIndex:::'+hasIndex);
        //prompt('index:::'+index);
        // prompt(index);
        document.getElementById('answer').style.backgroundImage = "url('./images/enterQuestion.png')";
        document.getElementById('exitrecord').style.backgroundImage = "url('./images/exit.png')";
        if(index == 0){
            //document.getElementById('prepage').
            document.getElementById('prepage').style.backgroundImage = "url('./images/record_top_active.png')";

            document.getElementById('nextpage').style.backgroundImage = "url('./images/record_bottom.png')";
            //prompt(document.getElementById('prepage').className);

            //document.getElementById('prepage').style.className = 'model active';
            //document.getElementById('nextpage').className = 'model';
        }else{
            //document.getElementById('prepage').className = 'model';
            //document.getElementById('nextpage').className = 'model active';

            document.getElementById('prepage').style.backgroundImage = "url('./images/record_top.png')";

            document.getElementById('nextpage').style.backgroundImage = "url('./images/record_bottom_active.png')";
        }

               /*var me = mod;
        var elem = $("#" + containerId).children("." + listName)
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
        if (hasIndex) {
            index = parseInt(hasIndex);
            // EVM.cookie.deleteValue('sdzIndex');
            hasIndex = '';
            if (index != length) {
                if (document.getElementsByClassName("active").length > 0) {
                    document.getElementsByClassName("active")[0].className = "model2";
                }
            } else {
                index = 0;
            }
        }
        if (index >= 0) {
            elem[index].className = elem[index].className + ' active';
        }*/
 
        // return me;
    };

    mod.clearCss = function () {
		var me = mod;
        var elem = $("#" + containerId).children("." + listName)
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
        }
		return me;
	};
    //赋值


    mod.moveH = function (directive) {
        var me = mod;
        if (directive == "backwards") { //左

        }
        else if (directive == 'forwards') {  //右
        }
        return me;
    };
    mod.moveU = function (directive) {
        var me = mod;
        if (directive == "up") { //上
           if(index==1){
               index --
               mod.toggleFocus();
           }
        }else if (directive == 'down') {  //下
            if(index==1){
                left.clearCss();
                cmsIndex.handle('right');
                right.toggleFocus(0)
            }else{
                index ++
                mod.toggleFocus();
            }
        }
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                break;
            case 'enter':
                if(index == 0){
                    if(page == 1){
                        return
                    }else{
                        page --
                        requestData()
                    }
                }else if(index == 1){
                    if(page >= totalPage){
                        return
                    }else{
                        page ++
                        requestData()
                    }
                }
            break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        //拦截返回键,方法返回isBlock是1或0,默认0不拦截
        var isBlock = 0;
        switch (key) {
            case keymap.Enrich.UP:
            case keymap.iPanel.UP:
            case keymap.DVN.UP:
                me.moveU('up');
                break;
            case keymap.Enrich.DOWN:
            case keymap.iPanel.DOWN:
            case keymap.DVN.DOWN:
                me.moveU('down');
                break;
            case keymap.Enrich.LEFT:
            case keymap.iPanel.LEFT:
            case keymap.DVN.LEFT:
                me.moveH('backwards');

                break;
            case keymap.Enrich.RIGHT:
            case keymap.iPanel.RIGHT:
            case keymap.DVN.RIGHT:

                me.moveH('forwards');

                break;
            case keymap.SELECT:
            case keymap.SELECT2:
                me.action('enter');
                break;
            case keymap.Enrich.BACK:
            case keymap.iPanel.BACK:
            case keymap.DVN.BACK:
                me.action('back');
                break;
            case keymap.Inspur.EXIT:
            case keymap.iPanel.EXIT:
            case 0:
                window.location.href = "address.html";
                break;
        }
        return isBlock;
    };

    return mod;
})();

cmsIndex.run();